package com.apartmentManage.service.impl;

import com.apartmentManage.Dao.ApartmentDao;
import com.apartmentManage.Dao.RoomDao;
import com.apartmentManage.entity.Apartment;
import com.apartmentManage.service.ApartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApartmentServiceImpl implements ApartmentService {

    @Autowired
    private ApartmentDao dao;

    @Autowired
    private RoomDao roomDao;

    @Override
    public List<Apartment> getAll() {
        return dao.getAll();
    }

    @Override
    public List<Apartment> getByRequirement(Apartment apartment) {
        return dao.getByRequirement(apartment);
    }

    @Override
    public Apartment getByNo(Integer apartmentNo) {
        return dao.getByNo(apartmentNo);
    }

    @Override
    public List<Integer> getAllNo() {
        List<Integer> allNo=new ArrayList<>();
        List<Apartment> all = dao.getAllNo();
        for (int i=0;i<all.size();i++){
            if ( "是".equals(all.get(i).getStatus())){
                allNo.add(all.get(i).getApartmentNo());
            }
        }
        return allNo;
    }

//    @Override
//    public List<String> getAllStatus() {
//        List<String> allNo=new ArrayList<>();
//        List<Apartment> all = dao.getAll();
//        for (int i=0;i<all.size();i++){
//            if ( "是".equals(all.get(i).getStatus())){
//                allNo.add(all.get(i).getApartmentNo());
//            }
//        }
//        return allNo;
//    }

    @Override
    public boolean add(Apartment apartment) {
//        获取现有的楼栋号
        List<Apartment> all = getAll();
//        判断是否有重复
        for (Apartment OldApartment : all){
            if (OldApartment.getApartmentNo()==apartment.getApartmentNo())
                return false;
        }
        return dao.add(apartment)>0;
    }

    @Override
    public boolean delete(Integer apartmentNo) {
       int num= dao.delete(apartmentNo);
//       将楼栋对应的宿舍信息也进行删除
       if (num>0)
           roomDao.deleteByApartmentNo(apartmentNo);
        return dao.delete(apartmentNo)>0;
    }

    @Override
    public boolean update(Apartment apartment) {
        /**
         * 检测是否有人居住
         */
        return dao.update(apartment)>0;
    }
}
